package service

import (
    "authserver/constants"
    "authserver/global"
	"authserver/model"
    "authserver/wechat"
    "errors"
	"time"
)

//
// RegAuth
//  @Description: 注册设备
//  @param key
//  @param authToken
//  @return err
//  @return authInfo
//
func RegAuth(key,authToken string) (err error,authInfo model.AuthInfo) {

	err=global.GVA_DB.Table("auth_info").Where("auth_key = ?",key).Find(&authInfo).Error
	if err!=nil{
		global.GVA_LOG.Error(err.Error())
		return err,authInfo
	}
	// check is exist

	if authInfo.AuthKey==""{ // QQ
		// xu yao zhu ce
		authInfo.AuthKey = key
		authInfo.AuthToken= authToken
		timestamp:=time.Now().Unix() + int64(global.GVA_CONFIG.System.DefaultTime * 3600)
		authInfo.LastTime = time.Unix(timestamp,0).Format(constants.DayTime)
        authInfo.RegTime=authInfo.LastTime
		err=global.GVA_DB.Create(&authInfo).Error
		if err!=nil{
			global.GVA_LOG.Error(err.Error())
			return err,authInfo
		}
	}

	// check exit time

	lastTime,err := time.Parse(constants.DayTime,authInfo.LastTime)

	if err!=nil{
		return
	}


	// no time out
	if lastTime.Before(time.Now()){
		//
		err = errors.New("is time out ")
		return err,authInfo
	}

	return nil,authInfo
}

func CheckUpdate(key string) (err error,versionResp model.VersionResp) {

    var authInfo model.AuthInfo

    err=global.GVA_DB.Table("auth_info").Where("auth_key = ?",key).Find(&authInfo).Error
    if err!=nil{
        global.GVA_LOG.Error(err.Error())
        return err,versionResp
    }

    var version model.SysVersion

    err=global.GVA_DB.Table("sys_version").Where("key","exe_version").Find(&version).Error

    if err!=nil{
        return err,versionResp
    }

    versionResp = model.VersionResp{
        Version: version.ParamsInt,
        Url:     version.Params,
    }

    return nil,versionResp
}

// @Title 游客登录
// @Description 函数的详细描述
// @Author 赵伟 2021/8/3 17:50
// @Param
// @Return

func AppletTouristLogin(appId string,param model.AppletTouristLogin) (err error, memberInfo model.AuthInfo) {

    // 游客登录
    // 使用 code 调用 api 获取 openid
    err, result := wechat.GetUserOpenID(appId,param.Code)
    if err != nil {
        global.GVA_LOG.Error(err.Error())
        return errors.New("网络错误"), memberInfo
    }
    errCode := result["errcode"]
    if errCode != 0 && errCode != nil {
        return errors.New(result["errmsg"].(string)), memberInfo
    }
    openid := result["openid"].(string)
    //sessionKey := result["session_key"].(string)
    //unionid := result["unionid"].(string)
    // TODO 临时随机生成openid和sessionKey

    global.GVA_DB.Table(memberInfo.TableName()).Where("open_id = ?",openid).Find(&memberInfo)

    if memberInfo.Id==0{
        // 没注册

        memberInfo=model.AuthInfo{
            Id:        0,
            AuthKey:   openid,
            AuthType:  2,
            AuthToken: "",
            LastTime:  "",
            RegTime:   time.Now().Format(constants.DayTime),
            OpenId:    openid,
            Status:    0,
        }

        err=global.GVA_DB.Create(&memberInfo).Error
        if err!=nil{
            global.GVA_LOG.Error(err.Error())
            return err,memberInfo
        }
    }

    return nil,memberInfo
}
